﻿//code by Heskey0
//https://space.bilibili.com/455965619
//https://github.com/Heskey0

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Teresa
{
    public class ManagerBase : EventBehaviour
    {

        /// <summary>
        /// 处理自身的消息
        /// </summary>
        /// <param name="eventCode">Event code.</param>
        /// <param name="message">Message.</param>
        public override void ProcessMsg(Enum eventCode, object message)
        {
            if (!dict.ContainsKey(eventCode))
            {
                Debug.LogWarning("没有注册 ： " + eventCode);
                return;
            }

            //一旦注册过这个消息 给所有的脚本 发过去
            List<EventBehaviour> list = dict[eventCode];
            for (int i = 0; i < list.Count; i++)
            {
                list[i].ProcessMsg(eventCode, message);
            }
        }



        /// <summary>
        /// 存储 event 和 哪个脚本 关联 的字典
        /// </summary>
        private Dictionary<Enum, List<EventBehaviour>> dict = new Dictionary<Enum, List<EventBehaviour>>();

        /// <summary>
        /// 添加事件
        /// </summary>
        /// <param name="eventCode">Event code.</param>
        /// <param name="event"></param>
        public ManagerBase Add(Enum eventCode, EventBehaviour @event)
        {
            List<EventBehaviour> list = null;

            //之前没有注册过
            if (!dict.ContainsKey(eventCode))
            {
                list = new List<EventBehaviour>();
                list.Add(@event);
                dict.Add(eventCode, list);
                return this;
            }

            //之前注册过
            list = dict[eventCode];
            list.Add(@event);
            return this;
        }

        /// <summary>
        /// 添加多个事件
        ///     一个脚本关心多个事件
        /// </summary>
        /// <param name="eventCode">Event code.</param>
        public void Add(Enum[] eventCodes, EventBehaviour @event)
        {
            for (int i = 0; i < eventCodes.Length; i++)
            {
                Add(eventCodes[i], @event);
            }
        }


        /// <summary>
        /// 移除事件
        /// </summary>
        /// <param name="eventCode">Event code.</param>
        /// <param name="event">Mono.</param>
        public void Remove(Enum eventCode, EventBehaviour @event)
        {
            //没注册过 没法移除 报个警告
            if (!dict.ContainsKey(eventCode))
            {
                Debug.LogWarning("没有这个事件" + eventCode + "注册");
                return;
            }

            List<EventBehaviour> list = dict[eventCode];

            if (list.Count == 1)
                dict.Remove(eventCode);
            else
                list.Remove(@event);
        }

        /// <summary>
        /// 移除多个
        /// </summary>
        /// <param name="eventCode">Event code.</param>
        /// <param name="event">Mono.</param>
        public void Remove(Enum[] eventCodes, EventBehaviour @event)
        {
            for (int i = 0; i < eventCodes.Length; i++)
            {
                Remove(eventCodes[i], @event);
            }
        }

    }
}
